package com.imooc.town.controller;

import com.imooc.town.converter.OrderForm2OrderDTOConverter;
import com.imooc.town.dto.OrderDTO;
import com.imooc.town.enums.ResultEnum;
import com.imooc.town.exception.SellException;
import com.imooc.town.form.OrderForm;
import com.imooc.town.service.BuyerService;
import com.imooc.town.service.OrderService;
import com.imooc.town.utils.ResultVOUtil;
import com.imooc.town.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * create by song on 2018/3/22
 * To change this template use File | Settings | File Templates.
 */

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {



    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;


    @PostMapping("/create")
    public ResultVo<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
        if (bindingResult.hasErrors ()){
            log.error ( "【创建订单】创建订单错误，orderForm{}",orderForm );
            throw new SellException ( ResultEnum.PARAM_ERROR.getCode (),bindingResult.getFieldError ().getDefaultMessage () );
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert ( orderForm );
        if (CollectionUtils.isEmpty ( orderDTO.getOrderDetailList () )){
            log.error ( "购物车不能为空" );
            throw  new SellException ( ResultEnum.CART_EMPTY );
        }

        OrderDTO result = orderService.createOrder ( orderDTO );
        Map map = new HashMap<>();
        map.put ( "orderId",result.getOrderId () );
        return ResultVOUtil.success ( map );
    }



    @GetMapping("/list")
    public ResultVo<OrderDTO> list(@RequestParam("openid") String openiid,
                                   @RequestParam(value = "page",defaultValue = "0") Integer page,
                                   @RequestParam(value = "size",defaultValue = "10") Integer size){

        if (StringUtils.isEmpty (openiid)){
            log.error ( "【查询订单列表】openid为空" );
            throw new SellException ( ResultEnum.PARAM_ERROR );
        }
        PageRequest request = new PageRequest ( page,size );
        Page<OrderDTO> orderDTOPage = orderService.findAllOrder ( openiid,request );
        return ResultVOUtil.success ( orderDTOPage );
    }


    @GetMapping("/detail")
    public ResultVo<OrderDTO> detail(@RequestParam("openid") String openid,@RequestParam("orderId")String orderId){
        OrderDTO orderDTO = buyerService.findOrderOne ( openid,orderId );
        return ResultVOUtil.success ( orderDTO );
    }

    @PostMapping("/cancle")
    public ResultVo<OrderDTO> cancle(String openid,String orderId){
        OrderDTO orderDTO = buyerService.cancleOrder ( orderId,orderId );
        return ResultVOUtil.success ();
    }




}
